<?php require_once "menu.php"; ?>
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%"><div class="pathway"></div></td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" href="javascript: document.getElementById('flag').value='add'; submitthis();">
                        <?php echo $html->image('admin/copy_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Create
                        </a>
                    </td>
                    <td>
                        <a class="toolbar" href="javascript: document.getElementById('flag').value='delete'; submitthis();">
                        <?php echo $html->image('admin/cancel_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Delete
                        </a>
                    </td>

                    <?php
                     if ($deleted) {
                     ?>
                    <td>
                        <a class="toolbar" href="javascript: document.getElementById('flag').value='recall'; submitthis();">
                        <?php echo $html->image('admin/restore_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Recall
                        </a>
                    </td>
                    <?php
                     }
                     ?>

                    <td>
                        <a class="toolbar" href="#">
                        <?php echo $html->image('admin/help_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br>
<br>
<div align="center" class="centermain">
    <div class="main">

        <table class="adminheading">
            <tr>
                <th class="info" rowspan="2" nowrap>Manage Tickets</th>
            </tr>
        </table>

        <form id="adminForm" name="adminForm" method="post" action="" enctype="multipart/form-data">
        <input type="hidden" id="flag" name="data[flag]" />


            <div id="tabs">
            	<ul>
            		<li><a href="#tabs-1">Open Tickets</a></li>
            		<li><a href="#tabs-2">My Ticket</a></li>
                    <?php
                     if ($bypass) {
                     ?>
                   <li><a href="#tabs-3">Deleted Tickets</a></li>
                   <?php
                     }
                     ?>            		
            	</ul>

                <div id="tabs-1" style="padding: 1px !important;">
                <table class="adminform" style="width: 100%;">
                    <tr>
                        <td width="80%" valign="top">

                            <div class="error"></div>
                            <table class="adminlist" cellpadding=20>
                            	<tr>
                            		<th colspan="7">Tickets</th>
                            		<th align="right">Show Records:&nbsp;<?php echo $form->select('limit', array('20' => '20', '50' => '50', '100' => '100'), $limit, array('onchange' => 'this.form.submit();')); ?></th>
                            	</tr>
                                <tr bgcolor="#ae1d1d">
                                    <td width="3%" class="subheader"><input type="checkbox" id="toggle1" onclick="checkAllId();"></td>
                                    <td width="5%" class="subheader" align="center"><?php echo $paginator->sort('Ticket#', 'id', array('style' => 'color: #ffffff;')); ?></td>
                                    <td width="20%" class="subheader" align="center"><?php echo $paginator->sort('Subject', 'subject', array('style' => 'color: #ffffff;')); ?></td>
                                    <td width="20%" class="subheader" align="center"><?php echo $paginator->sort('Owner', 'owner', array('style' => 'color: #ffffff;')); ?></td>
                                    <td width="10%" class="subheader" align="center"><?php echo $paginator->sort('Status', 'status', array('style' => 'color: #ffffff;')); ?></td>
                                    <td width="12%" class="subheader" align="center"><?php echo $paginator->sort('Date Created', 'date_added', array('style' => 'color: #ffffff;')); ?></td>
                                    <td width="12%" class="subheader" align="center"><?php echo $paginator->sort('Date Updated', 'date_reported', array('style' => 'color: #ffffff;')); ?></td>
                                    <td width="%" class="subheader">&nbsp;</td>
                                </tr>

                                <?php
                                 if ($data) {
                                    foreach($data as $k => $v){
                                        $color = (($k/2) == round($k/2)) ? "#efefef" : "#ffffff";
                                        if (date('Y-m-d', strtotime($v['Ticket']['date_added'])) < date('Y-m-d') && $v['Ticket']['status'] == 'Open') {
                                            $color = "#ff4f4f";
                                            $critical++;
                                        }

                                        if ($v['Ticket']['status'] == "Open") {
                                            $requests = array('take' => 'Take', 'assignto' => 'Assigned To');
                                        } else if ($v['Ticket']['status'] == "In Progress") {
                                            $requests = array('response' => 'Response', 'Assigned To');
                                        } else {
                                            $requests = array();
                                        }

                                        $tId = $v['Ticket']['ticketId'];
                                 ?>
                                <tr bgcolor="<?php echo $color; ?>">
                                    <td align=""><input type="checkbox" id="nt<?php echo $k; ?>" name="data[row][id][]" value="<?php echo $tId; ?>" onclick="isChecked(this.checked);"></td>
                                    <td align="center"><?php echo $tId; ?></td>
                                    <td align="">&#187;&nbsp;
                                        <?php
                                         if ($v['Ticket']['status'] == 'Closed') {
                                            echo $v['Ticket']['subject'];
                                         } else {
                                            echo $html->link($v['Ticket']['subject'], '/ticket/createticket/'.$v['Ticket']['ticketId']);     
                                         }

                                         if (is_null($v['Ticket']['date_updated'])) {
                                             $date_updated = "--";
                                         } else {
                                             $date_updated = date('M d, Y', strtotime($v['Ticket']['date_updated']));
                                         }
                                         ?>
                                    </td>
                                    <td align=""><?php echo $v['Ticket']['owner']; ?></td>
                                    <td align="center"><?php echo $v['Ticket']['status']; ?></td>
                                    <td align="center"><?php echo date('M d, Y', strtotime($v['Ticket']['date_added'])); ?></td>
                                    <td align="center"><?php echo $date_updated; ?></td>
                                    <td align="center">
                                        <?php
                                         if (count($requests)) {
                                             echo $form->select('request.assign', $requests, null,
                                                        array('id' => 'assign',
                                                              'style' => 'height: 23px; font-size: 14px; width: 150px;',
                                                              'onchange' =>
                                                                  "javascript: document.getElementById('flag').value=this.value; document.getElementById('ticketId').value={$v['Ticket']['ticketId']}; submitthis();"
                                                   ));
                                         }
                                         ?>
                                    </td>
                                </tr>
                                <?php
                                    }
                                 }
                                 ?>

                                <tr>
                                    <td colspan="8" align="center">
                                        <div class="pagination">
                                        <?php
                                         echo $paginator->prev()."&nbsp;";
                                         echo $paginator->numbers();
                                         echo "&nbsp;".$paginator->next();
                                         ?>
                                        </div>
                                    </td>
                                </tr>

                            </table>
                        </td>
                        <td width="20%" valign="top">
                            <table class="adminlist" cellpadding=20>
                            	<tr>
                            		<th colspan="2">Stats</th>
                        		</tr>
                        		<tr>
                                    <td class="subheader">Ticket Status</td>
                                    <td class="subheader">Count</td>
                                </tr>
                                <tr>
                                    <td>Open</td>
                                    <td align="center"><?php echo $html->link($stats[0][0]['open'], '/ticket/ticketmgnt/open') ?></td>
                                </tr>
                                <tr>
                                    <td>In Progress</td>
                                    <td align="center"><?php echo $html->link($stats[0][0]['in_progress'], '/ticket/ticketmgnt/inprogress'); ?></td>
                                </tr>
                                <tr>
                                    <td>Resolved / Pending Confirmation</td>
                                    <td align="center"><?php echo $html->link($stats[0][0]['resolved'], '/ticket/ticketmgnt/resolved'); ?></td>
                                </tr>
                    		</table>
                    		<table class="adminlist">
                                <tr>
                                    <td class="subheader">Critical</td>
                                    <td class="subheader" align="right"><?php echo $html->link($critical[0][0]['critical'], '/ticket/ticketmgnt/critical'); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                </tr>
                            </table>
                            <table class="adminlist">
                                <tr>
                                    <th colspan="2">Search</th>
                                </tr>
                                <tr>
                                    <td>Ticket #</td>
                                    <td><input type="text" name="data[search][ticketno]" style="width: 95%" /></td>
                                </tr>
                                <tr>
                                    <td>Subject</td>
                                    <td><input type="text" name="data[search][subject]" style="width: 95%" /></td>
                                </tr>
                                <tr>
                                    <td>Status</td>
                                    <td><?php echo $form->select('search.status', $status, null, array('style' => 'width: 99%; height: 22px;')); ?></td>
                                </tr>
                                <tr>
                                    <td>Owner</td>
                                    <td><input type="text" name="data[search][person]" style="width: 95%" /></td>
                                </tr>
                                <tr>
                                    <td colspan="2">Date Created</td>
                                </tr>
                                <tr>
                                    <td>From</td>
                                    <td><input type="text" name="data[search][date_added]" id="create_date" value="" style="width: 70%;" /><button type="reset" id="t_trigger_a">...</button></td>
                                </tr>
                                <tr>
                                    <td>To</td>
                                    <td><input type="text" name="data[search][date_ended]" id="end_date" value="" style="width: 70%;" /><button type="reset" id="t_trigger_b">...</button></td>
                                </tr>
                                <tr>
                                    <td colspan="2"><input type="submit" value="Search" class="button" style="width: 100%;" /></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

                </div>
                <div id="tabs-2" style="padding: 1px !important;">
                    <table class="adminlist">
                        <tr>
                            <td valign="top">
                                <table class="adminlist" cellpadding="20" style="width: 80%">
                                	<tr>
                                		<th colspan="5">Tickets</th>
                                	</tr>
                                    <tr bgcolor="#ae1d1d">
                                        <td width="3%" class="subheader">Ticket#</td>
                                        <td width="20%" class="subheader" align="center">Subject</td>
                                        <td width="20%" class="subheader" align="center">Status</td>
                                        <td width="12%" class="subheader" align="center">Date Created</td>
                                        <td width="12%" class="subheader" align="center">Date Updated</td>
                                    </tr>

                                    <?php
                                     if ($mine) {
                                        foreach($mine as $k => $v){
                                            $color = (($k/2) == round($k/2)) ? "#efefef" : "#ffffff";
                                            $tId = $v['ticketId'];
                                            if (is_null($v['date_updated'])) {
                                                $date_updated = "--";
                                            } else {
                                                $date_updated = date('M d, Y', strtotime($v['date_updated']));
                                            }
                                     ?>
                                    <tr bgcolor="<?php echo $color; ?>">
                                        <td align="center"><?php echo $v['ticketId']; ?></td>
                                        <td align="">&#187;&nbsp;<?php echo $html->link($v['subject'], '/ticket/ticketresponse/'.$v['ticketId']); ?></td>
                                        <td align="center"><?php echo $v['status']; ?></td>
                                        <td align="center"><?php echo date('M d, Y', strtotime($v['date_added'])); ?></td>
                                        <td align="center"><?php echo $date_updated; ?></td>
                                    </tr>
                                    <?php
                                        }
                                     }
                                     ?>
                                </table>
                            </td>
                            <td valign="top"></td>
                        </tr>
                    </table>
                </div>

                <?php
                 if ($bypass) {
                 ?>
                <div id="tabs-3" style="padding: 1px !important;">
                    <table class="adminlist">
                        <tr>
                            <td valign="top">
                                <table class="adminlist" cellpadding="20" style="width: 80%">
                                    <tr>
                                        <th colspan="6">Tickets</th>
                                    </tr>

                                    <tr>
                                        <td width="3%" class="subheader"><input type="checkbox" id="toggle2" onclick="checkAlldId();"></td>
                                        <td width="3%" class="subheader" align="center">Ticket#</td>
                                        <td width="20%" class="subheader" align="left">Subject</td>
                                        <td width="10%" class="subheader" align="center">Priority</td>
                                        <td width="12%" class="subheader" align="center">Date Added</td>
                                        <td width="12%" class="subheader" align="center">Date Deleted</td>
                                    </tr>

                                <?php
                                 if ($deleted) {
                                     foreach ($deleted as $x => $d) {
                                         $dcolor = (($x/2) == round($x/2)) ? "#efefef" : "#ffffff";
                                         if (is_null($d['date_updated'])) {
                                             $date_updated = "--";
                                         } else {
                                             $date_updated = date('M d, Y', strtotime($d['date_updated']));
                                         }
                                 ?>
                                <tr bgcolor="<?php echo $dcolor; ?>">
                                    <td align=""><input type="checkbox" id="d<?php echo $x; ?>" name="data[row][id][]" value="<?php echo $d['ticketId']; ?>" onclick="isChecked(this.checked);"></td>
                                    <td align="center"><?php echo $d['ticketId']; ?></td>
                                    <td align="left"><?php echo $d['subject']; ?></td>
                                    <td align="center"><?php echo ($d['priority'])?$d['priority']:"--"; ?></td>
                                    <td align="center"><?php echo date('M d, Y', strtotime($d['date_added'])); ?></td>
                                    <td align="center"><?php echo $date_updated; ?></td>
                                </tr>
                                <?php
                                     }
                                 }
                                 ?>

                                </table>
                            </td>
                        </tr>
                    </table>
                </div>
               <?php
                 }
                 ?>
            </div>


        <input type="hidden" name="boxchecked" value="0">
        <input type="hidden" id="ticketId" name="data[ticketId]">
        </form>

    </div>
</div>

<script type="text/javascript">

// jquery ui tabs
$(function() {
    $("#tabs").tabs();
});

function checkAllId()
{
    var num = '<?php echo $count; ?>';
    checkAll(num, 'nt', 'toggle1');
}

function checkcount()
{
    var cnt = '<?php echo $count; ?>';
    if (cnt == 0) {
        alert('No result found');
    }
}

function checkAlldId()
{
    var num = '<?php echo $dcount; ?>';
    checkAll(num, 'd', 'toggle2');
}

function checkmsg()
{
    var msg = '<?php echo $msg; ?>';
    if (msg) {
        alert(msg);
    }
}

checkcount();
checkmsg();

Calendar.setup({
    inputField     :    "create_date",      // id of the input field
    ifFormat       :    "%m/%d/%Y",       // format of the input field
    showsTime      :    false,            // will display a time selector
    button         :    "t_trigger_a",   // trigger for the calendar (button ID)
    singleClick    :    false,           // double-click mode
    step           :    1                // show all years in drop-down boxes (instead of every other year as default)
});
Calendar.setup({
    inputField     :    "end_date",      // id of the input field
    ifFormat       :    "%m/%d/%Y",       // format of the input field
    showsTime      :    false,            // will display a time selector
    button         :    "t_trigger_b",   // trigger for the calendar (button ID)
    singleClick    :    false,           // double-click mode
    step           :    1                // show all years in drop-down boxes (instead of every other year as default)
});

function submitthis()
{
    var flag = document.getElementById('flag').value;
    switch (flag) {
        case "delete":
            var msg = "Are you sure you want to delete the selected ticket?";
            if (confirm(msg)) {
                document.adminForm.submit();
            }
            break;

        case "recall":
            var msg = "Are you sure you want to recall the selected ticket?";
            if (confirm(msg)) {
                document.adminForm.submit();
            }
            break;

        default:
            document.adminForm.submit();
            break;
    }
}
</script>